package com.qian.apiserver.service.serviceImpl;


import com.qian.apiserver.dao.UserDao;
import com.qian.apiserver.entity.GmMatchRecord;
import com.qian.apiserver.entity.GmUser;
import com.qian.apiserver.model.MatchImageModel;
import com.qian.apiserver.model.User;
import com.qian.apiserver.service.UserServiceBase;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;


@Service("SysUserServiceImpl")
public class UserServiceImpl extends UserServiceBase {
    @Autowired
    private UserDao dao;

    public PageResp<User> find(PageReq pageReq, final User user) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "createTime"));
        Page<GmUser> page = dao.findAll(new Specification<GmUser>() {
            public Predicate toPredicate(Root<GmUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(user.getUsername()))
                    condition = cb.and(condition,cb.like(root.<String>get("username"),"%"+user.getUsername()+"%"));
                if(isNotEmpty(user.getUserCode()))
                    condition = cb.and(condition,cb.equal(root.<Integer>get("userCode"),user.getUserCode()));
                if(isNotEmpty(user.getStatus()))
                    condition = cb.and(condition,cb.equal(root.<Byte>get("status"),user.getStatus()));
                return condition;
            }
        },pageRequest);
        PageResp<User> pageResp = page2pageResp(page);
        return pageResp;
    }

    @Override
    protected User entity2model(GmUser entity) {
        User model = null;
        if(isNotEmpty(entity)){
            model = new User();
            model.setId(entity.getId());
            model.setSex(entity.getSex());
            model.setNotBindChip(entity.getNotBindChip());
            model.setBindChip(entity.getBindChip());
            model.setGemstone(entity.getGemstone());
            model.setWin(entity.getWin());
            model.setCount(entity.getCount());
            model.setPhone(entity.getPhone());
            model.setSpreadMany(entity.getSpreadMany());
            model.setBankCard(entity.getBankCard());
            model.setCardholder(entity.getCardholder());
            model.setSignDay(entity.getSignDay());
            model.setBestCard(entity.getBestCard());
            model.setQQ(entity.getQQ());
            model.setWX(entity.getWX());
            model.setUserCode(entity.getUserCode());
            model.setCountChip(entity.getCountChip());
            model.setSignMonth(entity.getSignMonth());
            model.setSignContinuous(entity.getSignContinuous());
            model.setCreateTime(entity.getCreateTime());
            model.setHeadUrl(entity.getHeadUrl());
            model.setNickName(entity.getNickName());
            model.setPassword(entity.getPassword());
            model.setStatus(entity.getStatus());
            model.setUsername(entity.getUsername());
        }
        return model;
    }

    @Override
    protected GmUser model2Entity(User model) {
        GmUser entity = null;
        if(isNotEmpty(model)){
            entity = new GmUser();
            entity.setId(model.getId());
            entity.setSex(model.getSex());
            entity.setNotBindChip(model.getNotBindChip());
            entity.setBindChip(model.getBindChip());
            entity.setGemstone(model.getGemstone());
            entity.setWin(model.getWin());
            entity.setCount(model.getCount());
            entity.setPhone(model.getPhone());
            entity.setSpreadMany(model.getSpreadMany());
            entity.setBankCard(model.getBankCard());
            entity.setCardholder(model.getCardholder());
            entity.setSignDay(model.getSignDay());
            entity.setBestCard(model.getBestCard());
            entity.setQQ(model.getQQ());
            entity.setWX(model.getWX());
            entity.setUserCode(model.getUserCode());
            entity.setCountChip(model.getCountChip());
            entity.setSignMonth(model.getSignMonth());
            entity.setSignContinuous(model.getSignContinuous());
            entity.setCreateTime(model.getCreateTime());
            entity.setHeadUrl(model.getHeadUrl());
            entity.setNickName(model.getNickName());
            entity.setPassword(model.getPassword());
            entity.setStatus(model.getStatus());
            entity.setUsername(model.getUsername());
        }
        return entity;
    }

    @Override
    protected GmUser updateParams(User model) {
        GmUser entity = dao.findOne(model.getId());
        if(isNotEmpty(model.getSex()))
            entity.setSex(model.getSex());
        if(isNotEmpty(model.getNotBindChip()))
            entity.setNotBindChip(model.getNotBindChip());
        if(isNotEmpty(model.getBindChip()))
            entity.setBindChip(model.getBindChip());
        if(isNotEmpty(model.getGemstone()))
            entity.setGemstone(model.getGemstone());
        if(isNotEmpty(model.getWin()))
            entity.setWin(model.getWin());
        if(isNotEmpty(model.getCount()))
            entity.setCount(model.getCount());
        if(isNotEmpty(model.getPhone()))
            entity.setPhone(model.getPhone());
        if(isNotEmpty(model.getSpreadMany()))
            entity.setSpreadMany(model.getSpreadMany());
        if(isNotEmpty(model.getBankCard()))
            entity.setBankCard(model.getBankCard());
        if(isNotEmpty(model.getCardholder()))
            entity.setCardholder(model.getCardholder());
        if(isNotEmpty(model.getSignDay()))
            entity.setSignDay(model.getSignDay());
        if(isNotEmpty(model.getBestCard()))
            entity.setBestCard(model.getBestCard());
        if(isNotEmpty(model.getQQ()))
            entity.setQQ(model.getQQ());
        if(isNotEmpty(model.getWX()))
            entity.setWX(model.getWX());
        if(isNotEmpty(model.getUserCode()))
            entity.setUserCode(model.getUserCode());
        if(isNotEmpty(model.getCountChip()))
            entity.setCountChip(model.getCountChip());
        if(isNotEmpty(model.getSignMonth()))
            entity.setSignMonth(model.getSignMonth());
        if(isNotEmpty(model.getSignContinuous()))
            entity.setSignContinuous(model.getSignContinuous());
        if(isNotEmpty(model.getCreateTime()))
            entity.setCreateTime(model.getCreateTime());
        if(isNotEmpty(model.getHeadUrl()))
            entity.setHeadUrl(model.getHeadUrl());
        if(isNotEmpty(model.getNickName()))
            entity.setNickName(model.getNickName());
        if(isNotEmpty(model.getPassword()))
            entity.setPassword(model.getPassword());
        if(isNotEmpty(model.getStatus()))
            entity.setStatus(model.getStatus());
        if(isNotEmpty(model.getUsername()))
            entity.setUsername(model.getUsername());
        return entity;
    }

}